package org.example.carsystem.service.impl;


import org.example.carsystem.domain.Menu;
import org.example.carsystem.mapper.MenuMapper;
import org.example.carsystem.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;


    @Override
    public List<Menu> lists(String menuName, String status) {
        System.err.println(status);
        return menuMapper.lists(menuName,status);
    }

    @Override
    public Map<String, Object> listss() {
        ArrayList<Menu> listall = new ArrayList<>();
        List<Menu> menuList = menuMapper.listss();
        List<Menu> collect = menuList.stream().filter(s -> s.getParentId() == 0).collect(Collectors.toList());
        for (Menu menu : collect) {
            listall.add(caidan(menuList,menu));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",listall);
        System.out.println(listall);
        return  map;

    }
    private Menu caidan(List<Menu> menuList, Menu menu) {
        ArrayList<Menu> list = new ArrayList<>();
        for (Menu menu1 : menuList) {
            if(menu1.getParentId()!=null && menu1.getParentId().equals(menu.getMenuId())){
                list.add(caidan(menuList,menu1));
            }
        }
        if(list.size()!=0){
            menu.setChildren(list);
        }

        return menu;
    }
    @Override
    public Integer adds(Menu menu) {
        return menuMapper.add(menu);
    }

    @Override
    public Menu getbyid(Integer id) {
        return menuMapper.getbyid(id);
    }

    @Override
    public Integer update(Menu menu) {
        return menuMapper.update(menu);
    }

    @Override
    public List<Menu> fuzzhsearch(String menuName) {
        List<Menu> listss = menuMapper.listss();

        //查询parentid的所有数据放到集合里
        List<Menu> collect = listss.stream().filter(ss -> ss.getParentId() == 0).collect(Collectors.toList());

        ArrayList<Menu> collecttree = new ArrayList<>();
        
        for (Menu menu : collect) {
            //使用递归 将所有集合中parentid与menuid一致的形成属性结构
           collecttree.add(searchtree(listss,menu));
        }
        //判断menuname是否为空
        if(!StringUtils.isEmpty(menuName)){
            //判断树形结构中与menuname一致的数据 不一致则删除
            treesearch(collecttree,menuName);
        }
       
        return collecttree;
    }

    private void treesearch(List<Menu> collecttree, String menuName) {
        Iterator<Menu> iterator = collecttree.iterator();
        //使用迭代器遍历集合
        while (iterator.hasNext()){
            //获取所有值
            Menu next = iterator.next();
            //查询与menuname不一致的数据
            if(!next.getMenuName().contains(menuName)){
                //获取查到数据的菜单                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     n
                List<Menu> children = next.getChildren();
                //如果没有直接删除
                if(CollectionUtils.isEmpty(children)){
                    iterator.remove();
                }else{
                    //递归 继续查询 有展示 没有删除
                    treesearch(children,menuName);
                    if(CollectionUtils.isEmpty(next.getChildren())){
                        iterator.remove();
                    }
                }
            }
        }
    }
    private Menu searchtree(List<Menu> listss, Menu menu) {
        ArrayList<Menu> list = new ArrayList<>();
        //循环几个将所有数据中parentid与menuid一致的数据放到几个钟
        for (Menu listss1 : listss) {
            if(listss1.getParentId()!=null && listss1.getParentId().equals(menu.getMenuId())){
                list.add(searchtree(listss,listss1));
            }
        }
        //如果几个不为空则将数据放到children中
        if(!list.isEmpty()){
            menu.setChildren(list);
        }
        return menu;
    }
    @Override
    public boolean deletebyidss(Integer menuId) {
        try {
            menuMapper.delete(menuId);
            deleteChlidren(menuId);
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void deleteChlidren(Integer menuId) {
        List<Integer>  list=menuMapper.getbypid(menuId);
        menuMapper.deleteByPid(menuId);
        if(!StringUtils.isEmpty(list)){
            for (Integer id : list) {
                deleteChlidren(id);
            }

        }
    }

    @Override
    public List<Menu> lookselect(Integer userId) {
       List<Menu> list= menuMapper.lookselect(userId);
        List<Menu> collect = list.stream().filter(ss -> ss.getParentId() == 0).collect(Collectors.toList());
        for (Menu menu : collect) {
            List<Menu> collect1 = list.stream().filter(ss -> ss.getParentId().equals(menu.getMenuId())).collect(Collectors.toList());
            menu.setChildren(collect1);
        }
        return collect;
    }


}
